package com.anlogic.sdk.templates.core.processes;

import org.eclipse.cdt.core.templateengine.TemplateCore;
import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import com.anlogic.sdk.project.settings.ProjectSettings;

public class AppendProjectSettings extends ProcessRunner  {
	public void process(TemplateCore template, ProcessArgument[] args, String processId, IProgressMonitor monitor) throws ProcessFailureException {
		String projectName = args[0].getSimpleValue();
		String nodeName = args[1].getSimpleValue();
		ProcessArgument[][] properties = args[2].getComplexArrayValue();
		
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		
		ProjectSettings projectSettings = new ProjectSettings(project);
		
		if (!projectSettings.projSettingsExists()) {
			projectSettings.createNewSettings();
		} else {
			projectSettings.loadProjectSettings();
		}
		
		for (ProcessArgument[] property : properties) {
			String name = property[0].getSimpleValue();
			String value = property[1].getSimpleValue();
			if (nodeName.equals(ProjectSettings.STRING_NODE)) {
				projectSettings.setStringNodeValue(name, value);
			}
		}
		
		projectSettings.saveProjectSettings();
	}
}
